<?php
if (!defined('__COLA_DIR__')) exit;

/**
 * 用户配置组件
 *
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Widget_Users_Profile extends Widget_Abstract_Users implements Widget_Interface_Do
{
    /**
     * 执行函数
     *
     * @access public
     * @return void
     */
    public function execute()
    {
        /** 注册用户以上权限 */
        $this->user->pass('member');
        $this->request->setParam('uid', $this->user->uid);
    }

    /**
     * 生成表单
     *
     * @access public
     * @return Form
     */
    public function form()
    {
        /** 构建表格 */
        $form = new Form($this->security->actionUrl('users-profile', false));

        /** 用户昵称 */
        $nickName = new Form_Text('nickName', NULL, NULL, _t('昵称'), _t('用户昵称可以与用户名不同, 用于前台显示.')
            . '<br />' . _t('如果你将此项留空, 将默认使用用户名.'));
        $form->addInput($nickName);

        /** 个人主页 */
        $url = new Form_Text('url', NULL, NULL, _t('个人主页'));
        $form->addInput($url);

        /** 签名 */
        $sign = new Form_Textarea('sign', NULL, NULL, _t('签名'));
        $form->addInput($sign);
		$sign->input->setAttr('style', 'min-height: 80px;');

        /** 简介 */
        $intro =  new Form_Textarea('intro', NULL, NULL, _t('简介'));
        $form->addInput($intro);
		$intro->input->setAttr('style', 'min-height: 120px;');

        /** 用户动作 */
        $do = new Form_Hidden('do', NULL, 'profile');
        $form->set($do);

        /** 提交按钮 */
        $submit = new Form_Submit('submit', NULL, _t('更新我的资料'));
        $form->set($submit);

        $nickName->value($this->user->nickName);
        $url->value($this->user->url);
		
        $sign->value($this->user->sign);
        $intro->value($this->user->intro);

        /** 给表单增加规则 */
        $nickName->addRule(array($this, 'nickNameExists'), _t('昵称已经存在'));
        $nickName->addRule('xssCheck', _t('请不要在昵称中使用特殊字符'));
        $url->addRule('url', _t('个人主页地址格式错误'));
        $sign->addRule('xssCheck', _t('请不要在签名中使用特殊字符'));
        $intro->addRule('xssCheck', _t('请不要在简介中使用特殊字符'));

		/** 插件接口 */
		//HOOK PLUG_Profile_Form($form, $this);
		
        return $form;
    }

    /**
     * 判断原密码是否正确
     *
     * @access public
     * @param integer $mid 分类主键
     * @return boolean
     */
    public function checkPassword($password)
    {
        return $this->user->password == Common::passwordHash($password);
    }

    /**
     * 生成表单
     *
     * @access public
     * @return Form
     */
    public function passwordForm()
    {
        /** 构建表格 */
        $form = new Form($this->security->actionUrl('users-profile', false));

        /** 用户旧密码 */
        $oldpassword = new Form_Password('oldpassword', NULL, NULL, _t('原密码'));
        $form->addInput($oldpassword);

        /** 用户密码 */
        $password = new Form_Password('password', NULL, NULL, _t('新密码'),  _t('建议使用特殊字符与字母、数字的混编样式,以增加系统安全性.'));
        $form->addInput($password);

        /** 用户密码确认 */
        $confirm = new Form_Password('confirm', NULL, NULL, _t('新密码确认'), _t('请确认你的密码, 与上面输入的密码保持一致.'));
        $form->addInput($confirm);

        /** 用户动作 */
        $do = new Form_Hidden('do', NULL, 'password');
        $form->set($do);

        /** 提交按钮 */
        $submit = new Form_Submit('submit', NULL, _t('更新密码'));
        $form->set($submit);

        $oldpassword->addRule('required', _t('必须填写旧密码'));
        $oldpassword->addRule(array($this, 'checkPassword'), _t('原密码错误'));
        $password->addRule('required', _t('必须填写新密码'));
        $password->addRule('minLength', _t('为了保证账户安全, 请输入至少六位的密码'), 6);
        $confirm->addRule('confirm', _t('两次输入的密码不一致'), 'password');

		/** 插件接口 */
		//HOOK PLUG_Password_Form($form, $this);
		
        return $form;
    }

    /**
     * 更新用户
     *
     * @access public
     * @return void
     */
    public function updateProfile()
    {
		$form = $this->form();
		
		/** 验证表单 */
        if ($message = $form->validate()) {
            $this->result->set(key($message), current($message));
        }

        /** 取出数据 */
        $user = $this->request->from(array_keys($form->getInputs()));
        $user['nickName'] = empty($user['nickName']) ? $user['name'] : $user['nickName'];

		/** 插件接口 */
		//HOOK $user | PLUG_Profile_Update($user, $this);

        /** 更新数据 */
        $this->update($this->user->uid, $user);
		
		$this->select()
		    ->where('table.users.uid = ?', $this->user->uid)
			->limit(1)
			->fetchRow(array($this, 'push'));
		
		/** 插件接口 */
		//HOOK PLUG_Profile_Update_Success($this);

        /** 提示信息 */
        $this->result->set(0, _t('您的资料已经更新'));
    }

    /**
     * 更新密码
     *
     * @access public
     * @return void
     */
    public function updatePassword()
    {
		$form = $this->passwordForm();

		/** 验证格式 */
        if ($message = $form->validate()) {
            $this->result->set(key($message), current($message));
        }

        /** 取出数据 */
        $user = $this->request->from(array_keys($form->getInputs()));

		/** 插件接口 */
		//HOOK $user | PLUG_Password_Update($user, $this);
		
        /** 更新数据 */
        $this->update($this->user->uid, $user);
		
		$this->select()
		    ->where('table.users.uid = ?', $this->user->uid)
			->limit(1)
			->fetchRow(array($this, 'push'));
		
		/** 插件接口 */
		//HOOK PLUG_Password_Update_Success($this);

        /** 提示信息 */
        $this->result->set(0, _t('密码已经成功修改'));
    }

    /**
     * 入口函数
     *
     * @access public
     * @return void
     */
    public function action()
    {
        $this->security->protect();
        $this->on($this->request->is('do=profile'))->updateProfile();
        $this->on($this->request->is('do=password'))->updatePassword();
		$this->result->set(404, _t('请求的资源不存在'));
    }
}
